
var path = require('path');
var fs = require('fs');
exports.index = function(req, res, next) {
	//?role=data到下个路由解析
	if(req.query.role=="data"){
		return next();
	}
	//res.local深merge
	res.locals={
		title:"首页",
	}
	//后面是浅merge，会覆盖已有属性
	res.render('index',{});
}
exports.openShop = function(req, res, next) {
	//res.local深merge
	if(req.query.role=="data"){
		return next();
	}
	res.locals={
		title:"开店"
	}
	//后面是浅merge，会覆盖已有属性
	res.render('openShop',{});
}
exports.myShop = function(req, res, next) {
	//res.local深merge
	if(req.query.role=="data"){
		return next();
	}
	res.locals={
		title:"我的店铺"
	}
	//后面是浅merge，会覆盖已有属性
	res.render('myShop',{});
}
exports.shopList = function(req, res, next) {
	//res.local深merge
	if(req.query.role=="data"){
		return next();
	}
	res.locals={
		title:"店铺列表"
	}
	//后面是浅merge，会覆盖已有属性
	res.render('shopList',{});
}
exports.shop = function(req, res, next) {
	//res.local深merge
	if(req.query.role=="data"){
		return next();
	}
	res.locals={
		title:"店铺"
	}
	//后面是浅merge，会覆盖已有属性
	res.render('shop',{});
}
exports.order = function(req, res, next) {
	//res.local深merge
	if(req.query.role=="data"){
		return next();
	}
	res.locals={
		title:"订单"
	}
	//后面是浅merge，会覆盖已有属性
	res.render('order',{});
}
exports.userCenter = function(req, res, next) {
	//res.local深merge
	if(req.query.role=="data"){
		return next();
	}
	res.locals={
		title:"我的外卖"
	}
	//后面是浅merge，会覆盖已有属性
	res.render('userCenter',{});
}
exports.login = function(req, res, next) {
	res.locals={
		title:"登陆",
	}
	res.render('login',{});
}
exports.register = function(req, res, next) {
	res.locals={
		title:"注册",
	}
	res.render('register',{});
}
/**
 * 获取模板获取对应设备view下params.page模块下的content.html。默认包含公共头部底部，
 * 请求带?partial=none时，则不包含;
 * @todo 可扩展，通过partial参数，自由组合返回html包含模块
 */
exports.tpl = function(req, res, next) {
	var viewDir=req.app.get('views');
	var contentDirectory=path.join(viewDir,req.params.page+'/content.html');
	var headerDirectory=path.join(viewDir,'common/header.html');
	var footerDirectory=path.join(viewDir,'common/footer.html');
	var content=fs.readFileSync(contentDirectory).toString();
	var header=fs.readFileSync(headerDirectory).toString();
	var footer=fs.readFileSync(footerDirectory).toString();
	var html=header+content+footer;
	if(req.query.partial=='none'){
		html=content;
	}
	res.send(html);
}
